package com.ahies.base;


import com.ahies.common.contanst.BaseEntityFieldConstant;
import com.ahies.utils.TableUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

public abstract class BaseController {

    protected Logger log = LoggerFactory.getLogger(getClass());

    public <T extends BaseEntity> Page<T> getPage(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }

        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }

        Page page = new Page<T>(Long.valueOf(pageNum), Long.valueOf(pageSize));
        String sortColumn = "";
        if (StringUtils.isBlank(request.getParameter("sortColumn"))) {
            sortColumn = BaseEntityFieldConstant.CREATE_TIME;
        } else {
            sortColumn = request.getParameter("sortColumn");
        }
        if (Objects.equals(request.getParameter("sortRule"), BaseEntityFieldConstant.SORT_ASC)) {
            page.setAsc(TableUtil.getColumnName(sortColumn));
        } else {
            page.setDesc(TableUtil.getColumnName(sortColumn));
        }
//        page.setDesc("CONVERT("+TableUtil.getColumnName(sortColumn)+" using gbk)");

        return page;
    }

    public <T extends BaseEntity> Page<T> getPage(HttpServletRequest request, T t) {
        String pageNum = request.getParameter("pageNum");
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }

        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        if (StringUtils.isBlank(t.getSortColumn())) {
            t.setSortColumn(BaseEntityFieldConstant.UPDATE_TIME);
        }
        Page page = new Page<T>(Long.valueOf(pageNum), Long.valueOf(pageSize));
        if (Objects.equals(t.getSortRule(), BaseEntityFieldConstant.SORT_ASC)) {
            page.setAsc(TableUtil.getColumnName(t.getSortColumn()));
        } else {
            page.setDesc(TableUtil.getColumnName(t.getSortColumn()));
        }
//        page.setDesc("CONVERT("+TableUtil.getColumnName(t.getSortColumn())+" using gbk)");
        return page;
    }


    public <T extends BaseEntity> QueryWrapper<T> getCreateTimeQueryWrapper(T t, QueryWrapper<T> wrapper) {
        if (StringUtils.isNotBlank(t.getStartTime())) {
            wrapper.ge(TableUtil.getColumnName(BaseEntityFieldConstant.CREATE_TIME), t.getStartTime());
        }
        if (StringUtils.isNotBlank(t.getEndTime())) {
            wrapper.le(TableUtil.getColumnName(BaseEntityFieldConstant.CREATE_TIME), t.getEndTime() + " 23:59:59");
        }
        return wrapper;
    }


    public <T extends BaseEntity> QueryWrapper<T> getUpdateTimeQueryWrapper(T t, QueryWrapper<T> wrapper) {
        if (StringUtils.isNotBlank(t.getStartTime())) {
            wrapper.ge(TableUtil.getColumnName(BaseEntityFieldConstant.UPDATE_TIME), t.getStartTime());
        }
        if (StringUtils.isNotBlank(t.getEndTime())) {
            wrapper.le(TableUtil.getColumnName(BaseEntityFieldConstant.UPDATE_TIME), t.getEndTime() + " 23:59:59");
        }
        return wrapper;
    }


    public <T extends BaseEntity> QueryWrapper<T> getTimeQueryWrapper(String timeColumn, T t, QueryWrapper<T> wrapper) {

        if (StringUtils.isNotBlank(t.getStartTime())) {
            wrapper.ge(timeColumn, t.getStartTime());
        }
        if (StringUtils.isNotBlank(t.getEndTime())) {
            wrapper.le(timeColumn, t.getEndTime() + " 23:59:59");
        }
        return wrapper;
    }


    public static ArrayList<Integer> toList(String ids) {
        ArrayList<Integer> list = new ArrayList<>();
        if (ids.contains(",")) {
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                list.add(Integer.parseInt(id.trim()));
            }
        } else {
            list.add(Integer.parseInt(ids.trim()));
        }
        return list;
    }

    public <T extends BaseEntity> QueryWrapper<T> getCreateTimeQueryWrapper(T t) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(t.getStartTime())) {
            wrapper.ge(TableUtil.getColumnName(BaseEntityFieldConstant.CREATE_TIME), t.getStartTime());
        }
        if (StringUtils.isNotBlank(t.getEndTime())) {
            wrapper.le(TableUtil.getColumnName(BaseEntityFieldConstant.CREATE_TIME), t.getEndTime()+" 23:59:59");
        }
        return wrapper;
    }


    public <T extends BaseEntity> QueryWrapper<T> getUpdateTimeQueryWrapper(T t) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(t.getStartTime())) {
            wrapper.ge(TableUtil.getColumnName(BaseEntityFieldConstant.UPDATE_TIME), t.getStartTime());
        }
        if (StringUtils.isNotBlank(t.getEndTime())) {
            wrapper.le(TableUtil.getColumnName(BaseEntityFieldConstant.UPDATE_TIME), t.getEndTime()+" 23:59:59");
        }
        return wrapper;
    }


    public <T extends BaseEntity> QueryWrapper<T> getTimeQueryWrapper(T t, String timeColumn) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(t.getStartTime())) {
            wrapper.ge(timeColumn, t.getStartTime());
        }
        if (StringUtils.isNotBlank(t.getEndTime())) {
            wrapper.le(timeColumn, t.getEndTime() +" 23:59:59");
        }
        return wrapper;
    }


}
